<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo2.png" width="14%" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- Aside区域 -->
      <el-aside :width="isCollape ? '60px' : '200px'">
        <!-- 动态绑定，如果折叠就缩小左侧宽度 -->
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <!-- active-text-color：选中颜色      :unique-opened="true" 同时只展示一个，也可以写成unique-opened -->
        <!-- :collapse="isCollape" 是否展开      :collapse-transition="false" ：不开启折叠展开动画 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollape"
          :collapse-transition="false"
          router    
          :default-active="defalutActive"
        >   <!-- 开启路由绑定，设置后点击就直接跳转到 index 设置的地址 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- :index ：唯一标识，如果不设置，每个标签都一样 -->
            <template slot="title">
              <!-- 一级菜单 -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu></el-aside
      >
      <!-- Main区域 -->
      <router-view></router-view>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 左侧一级标签图标样式
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 是否展开菜单的样式属性
      isCollape: false,
      // 高亮default-active值，设置 index属性值，也就是跳转的 path
      defalutActive: ''
    };
  },
  created() {
    this.getMenuList();
    this.defalutActive =window.sessionStorage.getItem('itemIndex')    // 页面刷新后立刻赋值，如果第一次加载就没有高亮
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg);
      }
      this.menuList = res.data;
    },

    // 展开关闭侧边栏
    toggleCollapse() {
      this.isCollape = !this.isCollape;
    },
    // 防止刷新后左侧栏高亮效果失效
    saveNavStatus(itemIndex){
      window.sessionStorage.setItem("itemIndex",itemIndex);   // 保存在 sessionStorage 防止刷新丢失
      this.defalutActive = itemIndex;   // 点击后立刻修改高亮属性值
    }
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  padding-top: 2px;
  align-items: center; /* 居中显示 */
  color: #ffff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
// 增加左侧菜单栏图标和字的距离
.iconfont {
  margin-right: 5px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 17px;
  line-height: 24px;
  color: #ffff;
  text-align: center;
  letter-spacing: 0.1em;
  cursor: pointer;
}
</style>